package com.lihao.wanandroid.ui.home

import android.os.Bundle
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.google.android.material.tabs.TabLayoutMediator
import com.lihao.jetpackcore.ext.getActivityViewModel
import com.lihao.jetpackcore.ext.nav
import com.lihao.wanandroid.R
import com.lihao.wanandroid.app.base.BaseFragment
import com.lihao.wanandroid.app.ext.init
import com.lihao.wanandroid.app.ext.setPageListener
import com.lihao.wanandroid.app.util.CacheUtils
import com.lihao.wanandroid.ui.home.plaza.PlazaFragment
import com.lihao.wanandroid.ui.home.publicaccount.PublicAccountFragment
import com.lihao.wanandroid.ui.home.recommend.RecommendFragment
import com.lihao.wanandroid.ui.main.MainViewModel
import com.lihao.wanandroid.ui.project.ProjectListFragment
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.android.synthetic.main.fragment_main.viewPager
import kotlinx.android.synthetic.main.include_home_toolbar.*

/**
 * Created by lihao on 2020/5/19.
 * Describe: 首页Fragment
 */
class HomeFragment : BaseFragment<HomeViewModel>() {

    private var fragments = arrayListOf<Fragment>()
    private val recommendFragment: RecommendFragment by lazy { RecommendFragment() }
    private val recommendFragment1: RecommendFragment by lazy { RecommendFragment() }
    private val plazaFragment: PlazaFragment by lazy { PlazaFragment() }
    private val publicAccFragment: PublicAccountFragment by lazy { PublicAccountFragment() }
    private val newProjectFragment: ProjectListFragment by lazy {
        ProjectListFragment.newInstance(
            true
        )
    }

    private val mainViewModel: MainViewModel by lazy { getActivityViewModel<MainViewModel>() }

    init {
        fragments.apply {
            add(recommendFragment)
            add(recommendFragment1)
            add(plazaFragment)
            add(publicAccFragment)
            add(newProjectFragment)
        }
    }

    override fun layoutId() = R.layout.fragment_home

    override fun initView(savedInstanceState: Bundle?) {
        viewPager.init(this, fragments, true)
        TabLayoutMediator(tabLayout, viewPager) { tab, position ->
            when (position) {
                0 -> tab.text = "最新"
                1 -> tab.text = "推荐"
                2 -> tab.text = "广场"
                3 -> tab.text = "公众号"
                4 -> tab.text = "新项目"
            }
        }.attach()

        viewPager.currentItem = mViewModel.curPage
        viewPager.setPageListener {
            mViewModel.curPage = it
        }
        tvSearch.setOnClickListener {
            nav().navigate(R.id.action_mainFragment_to_searchFragment)
        }
        ivHeaderPhoto.setOnClickListener {
            if (CacheUtils.isLogin()) {
                mViewModel.curPage = 4
                mainViewModel.setCurPage(mViewModel.curPage)
            } else {
                nav().navigate(R.id.action_to_loginFragment)
            }
        }

        // 显示用户信息
        CacheUtils.getUserInfo()?.let {
            Glide.with(this)
                .load("http://5b0988e595225.cdn.sohucs.com/q_70,c_zoom,w_640/images/20190115/87868f21befc4e7f9007aa71efa79621.jpeg")
                .placeholder(R.drawable.ic_default_photo)
                .apply(RequestOptions.bitmapTransform(CircleCrop()))
                .into(ivHeaderPhoto)
        }
    }

    override fun lazyLoadData() {

    }


}